package com.tfjybj.itoo.exam.provider.service.impl;

import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.PaperRecordEntity;
import com.tfjybj.itoo.exam.entity.TemplateDetailsEntity;
import com.tfjybj.itoo.exam.model.*;
import com.tfjybj.itoo.exam.provider.dao.PaperRecordDao;
import com.tfjybj.itoo.exam.provider.service.OnlineExamService;
import com.tfjybj.itoo.exam.provider.service.PaperRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * PaperRecordService接口实现类
 * ${base}表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Service("paperRecordService")
public class PaperRecordServiceImpl extends BaseServicePlusImpl<PaperRecordDao, PaperRecordEntity> implements PaperRecordService {


    @Resource
    private PaperRecordDao paperRecordDao;

    @Resource
    private OnlineExamService onlineExamService;

    @Override
    public PageInfo<PaperRecordEntity> queryByLikeExamineeId(String examineeId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(paperRecordDao.queryLikeExamineeId(examineeId));
    }


    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 更新每小题分数
     *
     * @param paperRecordEntity
     * @return
     * @author 侯旭日
     * @since 2.0.0 2018-11-23 19:48:2
     */
    @Override
    public int updateManualJudge(PaperRecordEntity paperRecordEntity) {
        return paperRecordDao.updateById(paperRecordEntity);
    }

    /**
     * 查询试卷详情--樊建华--2018年11月22日20:53:15
     *
     * @param templateId 试卷id
     * @return 试卷详情
     */
    @Override
    public ExamPaperModel selectPaperInfoByPaperRecord(String templateId) {
        return paperRecordDao.selectPaperInfoByPaperRecord(templateId);
    }

    @Override
    public List<QuestionMainModel> selectMainInfoByPaperRecord(String templateId, String studentId) {
        return paperRecordDao.selectMainInfoByPaperRecord(templateId, studentId);
    }

    @Override
    public List<QuestionMainModel> selectQuestionByChildId(String templateId, String examineeId) {
        return paperRecordDao.selectQuestionByChildId(templateId, examineeId);
    }

    @Override
    public List<TemplateDetailsEntity> selectQuestionTypeByExamineeId(String examineeId){
        return paperRecordDao.selectQuestionTypeByExamineeId(examineeId);
    }

    @Override
    public List<PaperRecordEntity> selStuDetails(String examineeId, String templateId) {
        return paperRecordDao.selStuDetails(examineeId, templateId);
    }


    @Override
    public int insertAllPaperRecord(List<PaperRecordEntity> recordEntityList, String examineeId, String examinationId) {
        return paperRecordDao.insertAllPaperRecord(recordEntityList, examineeId, examinationId);
    }

    @Override
    public List<PaperRecordModel> queryRecordStudentAnswer(String emaleeId, String templateId, String courseId) {
        return paperRecordDao.queryRecordStudentAnswer(emaleeId, templateId, courseId);
    }

    @Override
    public List<ExamineeModel> selectExamineeByQuestionId(String wrongQuestionId) {
        return paperRecordDao.selectExamineeByQuestionId(wrongQuestionId);
    }

    /**
     * 更新错题对应的新题到表
     *
     * @param examineeId      答题记录的Id
     * @param templateId      新题的id
     * @param wrongQuestionId 答题记录的Id
     * @author 任蒙蒙
     * @since 2.0.0 2018-11-17 17:10:28
     */
    @Override
    public PaperRecordEntity selectPaperRecordId(String examineeId, String templateId, String wrongQuestionId) {
        return paperRecordDao.selectPaperRecordId(examineeId, templateId, wrongQuestionId);
    }

    @Override
    public List<String> queryPaperRecordId(String examineeId, String examinationId) {
        return paperRecordDao.queryPaperRecordId(examineeId, examinationId);
    }

    /**
     * 查询考试所有试题的正确率
     *
     * @param examID 考试ID
     * @return
     * @author 魏恩君
     * @since 2.0.0 2019/1/26
     */
    @Override
    public List<PaperRecordModel> queryQuestionCorrectRate(String examID) {
        List<PaperRecordModel> list = paperRecordDao.queryQuestionCorrectRate(examID);
        for (PaperRecordModel paperRecord : list) {
            paperRecord.setQuestionContent(paperRecord.getQuestionContent().substring(0, 5));
            paperRecord.setCorrectRate(String.valueOf(Math.round(Double.valueOf(paperRecord.getCorrectRate()) * 100)));
        }
        return list;
    }

    /**
     * 课程练习-根据用户id查询该用户做题记录
     *
     * @param examineeId
     * @return 是否查询成功
     * @ahthor 贾建清
     * @since 2.0.0 2019-3-27 09:05:19
     */
    @Override
    public List<CoursePaperRecordModel> queryQuestionStudentAnswer(String examineeId) {
        return paperRecordDao.queryQuestionStudentAnswer(examineeId);
    }


    /**
     * 课程练习-根据用户id和题干id更新用户答案
     *
     * @param examineeId
     * @param questionId
     * @param coursePaperRecordModel
     * @return 是否更新成功
     * @anthor 贾建清
     * @since 2.0.0 2019-3-27 10:58:24
     */
    @Override
    public boolean updateStudentAnswer(String examineeId, String questionId, CoursePaperRecordModel coursePaperRecordModel) {
        return paperRecordDao.updateStudentAnswer(examineeId, questionId, coursePaperRecordModel);
    }
}
